﻿using System;
using System.Web;
using System.Drawing;
using Microsoft.SharePoint;
using System.IO;
using SPThumbnailExtender.Interne;

namespace SPThumbnailExtender
{
    namespace Handler
    {

        public class MobileImageHandler : IHttpHandler
        {
            /// <summary>
            /// You will need to configure this handler in the web.config file of your 
            /// web and register it with IIS before being able to use it. For more information
            /// see the following link: http://go.microsoft.com/?linkid=8101007
            /// </summary>
            #region IHttpHandler Members

            public bool IsReusable
            {
                // Return false in case your Managed Handler cannot be reused for another request.
                // Usually this would be false in case you have some state information preserved per request.
                get { return true; }
            }

            public void ProcessRequest(HttpContext context)
            {
                //write your handler implementation here.
                string strImg = context.Request.QueryString["img"];
                string strWidth = context.Request.QueryString["width"];
                string strHeight = context.Request.QueryString["height"];
                string strCount = context.Request.QueryString["count"];

                int? _imgWidth = null;
                int? _imgHeight = null;
                bool? _bCount = null;
                bool? _bCache = null;
                int imgWidth;
                if (int.TryParse(strWidth, out imgWidth))
                {
                    _imgWidth = imgWidth;
                }
                int imgHeight;
                if (int.TryParse(strHeight, out imgHeight))
                {
                    _imgHeight = imgHeight;
                }
                int _iPage = 0;

                bool bCache;
                if (bool.TryParse(context.Request["usecache"], out bCache))
                {
                    _bCache = bCache;
                }

                bool bCount;
                if (bool.TryParse(strCount, out bCount))
                {
                    _bCount = bCount;
                }
                HandlerHelper.ProcessRequest(context, strImg, _imgWidth, _imgHeight, _iPage, _bCount, _bCache, typeof(ThumbnailImage));

            }
            #endregion
        }
    }
}